package com.doctcloud.hisinfo.common.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.doctcloud.common.core.domain.R;
import com.doctcloud.common.core.utils.StringUtils;
import com.doctcloud.common.core.web.controller.BaseController;
import com.doctcloud.common.core.web.domain.AjaxResult;
import com.doctcloud.common.log.annotation.Log;
import com.doctcloud.common.log.enums.BusinessType;
import com.doctcloud.common.security.annotation.RequiresPermissions;
import com.doctcloud.hisinfo.common.service.IDeptDictService;
import com.doctcloud.hisinfo.histable.domain.comm.DeptDictDo;
import com.doctcloud.hisinfo.histable.domain.vo.DeptDictVo;
import com.doctcloud.system.api.domain.SysDept;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * @ClassName DeptDictController
 * @Author samula
 * @Descripe product by com.doctcloud
 * @Data 2025-09-28 11:52
 **/
@RestController
@RequestMapping(value = "/comm/")
public class DeptDictController extends BaseController {
    @Autowired
    private IDeptDictService deptDictService;

    /**
     * 新增科室
     * @param dept
     * @return
     * @throws IOException
     */
    @RequiresPermissions("system:dept:add")
    @Log(title = "科室管理", businessType = BusinessType.INSERT)
    @PostMapping("/dept/add")
    public AjaxResult add(@Validated @RequestBody DeptDictDo dept) throws IOException {
        String error = checkObjectValid(dept);
        if (error.length() > 0){
            return error(error);
        }
        if (deptDictService.checkDeptNameUnique(dept))
        {
            return error("新增部门'" + dept.getDeptName() + "'失败，部门名称已存在");
        }
        int rows = deptDictService.addDeptDict(dept);
        return toAjax(rows);
    }

    /**
     * 科室编辑
     * @param dept
     * @return
     * @throws IOException
     */
    @RequiresPermissions("system:dept:edit")
    @Log(title = "部门管理", businessType = BusinessType.UPDATE)
    @PutMapping("/dept/edit")
    public AjaxResult update(@Validated @RequestBody DeptDictDo dept) throws IOException {
        String error = checkObjectValid(dept);
        if (error.length() > 0){
            return AjaxResult.error(error);
        }
        return toAjax(deptDictService.updateDeptDict(dept));
    }

    /**
     * 删除
     * @param dept
     * @return
     * @throws IOException
     */
    @RequiresPermissions("system:dept:remove")
    @Log(title = "科室管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/dept/delete")
    public AjaxResult delete(@RequestBody DeptDictDo dept) throws IOException {
        String error = checkObjectValid(dept);
        if (error.length() > 0){
            return AjaxResult.error(error);
        }
        return toAjax(deptDictService.deleteDeptDict(dept.getDeptCode()));
    }
    /**
     * 查询部门列表（排除节点）
     *
     */
//    @RequiresPermissions("system:dept:list")
    @PostMapping("/dept/list")
    @ResponseBody
    public AjaxResult list(SysDept dept) throws IOException {
        System.out.println(dept.toString());

        List<DeptDictVo> list = deptDictService.listESDepts();
        return AjaxResult.success(list);
    }
    /**
     * 查询部门列表（排除节点）
     */
    @RequiresPermissions("system:dept:list")
    @GetMapping("/dept/listPage")
    @ResponseBody
    public AjaxResult listPage(@Param("curPage") int curPage, @Param("pageSize") int pageSize) throws IOException {
        int validPage = Math.max(curPage, 1);
        int validSize = Math.max(pageSize, 10); // 最小每页10条，最大可限制为1000
        IPage<DeptDictDo> deptDictDoIPage = deptDictService.pageESDepts(validPage, validSize);
        return AjaxResult.success(deptDictDoIPage);
    }

    @GetMapping("/dept/dblist")
    public R<List<DeptDictDo>> listDeptByDB(){
        return R.ok(deptDictService.listDeptsDB());
    }

    @GetMapping("/dept/dbquerybycode/{deptCode}")
    public R<DeptDictDo> dbqueryDeptByDB(@PathVariable("deptCode") String deptCode){
        return R.ok(deptDictService.selectDeptByCode(deptCode));
    }

    private String checkObjectValid(DeptDictDo dept){
        String errorInfo = "";
        if (ObjectUtils.isEmpty(dept)){
            return "空数据无法处理";
        }
        if (StringUtils.isEmpty(dept.getDeptName()) || StringUtils.isEmpty(dept.getDeptCode()) || StringUtils.isEmpty(dept.getHospitalId())){
            return "无效传值,[科室名称、科室代码、医院标识]不得为空";
        }
        return errorInfo;
    }

}
